package com.wxl.xueyuan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.wxl.xueyuan.MyApp;
import com.wxl.xueyuan.R;
import com.wxl.xueyuan.activity.SearchActivity;
import com.wxl.xueyuan.adapter.HomeAdapter;
import com.wxl.xueyuan.entity.CategoryEntity;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles;
    private ViewPager viewPager;
    private SlidingTabLayout slidingTabLayout;
    private ImageButton enter;
    private EditText etSearch;

    public HomeFragment() {
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        viewPager = mRootView.findViewById(R.id.fixedViewPager);
        slidingTabLayout = mRootView.findViewById(R.id.slidingTabLayout);
        enter = mRootView.findViewById(R.id.enter);
        etSearch = mRootView.findViewById(R.id.et_search);
    }

    @Override
    protected void initData() {
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyword = etSearch.getText().toString().trim();
                Bundle bundle=new Bundle();
                bundle.putString("keyword", keyword);
                Intent in = new Intent(getActivity(), SearchActivity.class);
                in.putExtras(bundle);
                startActivity(in);
            }
        });
        getArticleCategoryList();
    }

    private void getArticleCategoryList() {
        List<CategoryEntity> list = new ArrayList<>();
        if ((Integer)MyApp.getmData("cg0") == 1)
            list.add(new CategoryEntity(0, "计算机学院新闻"));
        if ((Integer)MyApp.getmData("cg1") == 1)
            list.add(new CategoryEntity(1, "计算机学院通知"));
        if ((Integer)MyApp.getmData("cg2") == 1)
            list.add(new CategoryEntity(2, "软件学院新闻"));
        if ((Integer)MyApp.getmData("cg3") == 1)
            list.add(new CategoryEntity(3, "软件学院通知"));
        if ((Integer)MyApp.getmData("cg4") == 1)
            list.add(new CategoryEntity(4, "教务处新闻"));
        if ((Integer)MyApp.getmData("cg5") == 1)
            list.add(new CategoryEntity(5, "教务处通知"));

        mTitles = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            mTitles[i] = list.get(i).getCategoryName();
            mFragments.add(ArticleFragment.newInstance(list.get(i).getCategoryId()));
        }
        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.setAdapter(new HomeAdapter(getFragmentManager(), mTitles, mFragments));
        slidingTabLayout.setViewPager(viewPager);
    }
}